package com.ds.infrastructure.audit.common.entity;

import com.ds.infrastructure.audit.common.constant.CommonConstant;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 作用：
 *
 * @author WeiShaoying
 * @date 2020/7/31
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(value = JsonInclude.Include.NON_EMPTY)
public class Result<T> implements Serializable {

    private static final long serialVersionUID = -7900628714450083718L;

    /**
     * 状态码
     */
    private Integer code;

    /**
     * 描述信息
     */
    private String msg;

    /**
     * 数据
     */
    private T data;


    private Result(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    /**
     * 成功时调用
     *
     * @return
     */
    public static Result success() {
        return Result.builder().code(200).msg(CommonConstant.SUCCESS).build();
    }

    /**
     * 成功时调用
     *
     * @return
     */
    public static <T> Result success(T data) {
        return Result.builder().code(200).msg(CommonConstant.SUCCESS).data(data).build();
    }

    /**
     * 失败时调用，静态方法
     *
     * @param code 错误码
     * @param msg  错误消息
     * @return
     */
    public static Result fail(Integer code, String msg) {
        return new Result(code, msg);
    }

}
